package com.sgb.kjwl.viewmodel.machine;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.AskLeaseFactoryEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseItemEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MLog;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AskLeaseFactoryModel2 extends ViewModel {

    public MutableLiveData<Boolean> showLoad = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> initAddressDataLiveData = new MutableLiveData<>();

    public MutableLiveData<String> msgLiveData = new MutableLiveData<>();

    public MutableLiveData<String> publishLiveData = new MutableLiveData<>();
    public MutableLiveData<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>> showTypeViewLiveData = new MutableLiveData<>();
    public MutableLiveData<List<DictEntity3>> showAllPayWayLiveData = new MutableLiveData<>();
    public MutableLiveData<List<DictEntity3>> showPowerWayLiveData = new MutableLiveData<>();
    public MutableLiveData<List<DictEntity3>> showPurposeListLiveData = new MutableLiveData<>();
    public MutableLiveData<Integer> selectFragLiveData = new MutableLiveData<>();
    public MutableLiveData<AskLeaseFactoryEntity> showDetailLiveData = new MutableLiveData<>();//详情数据
    public AskLeaseFactoryEntity entity = new AskLeaseFactoryEntity();//发布数据

    //获取省市区
    public void getAddressModel() {
        showLoad.postValue(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                showLoad.postValue(false);
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK))
                    initAddressDataLiveData.postValue(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
               // DialogHelper.dismissProgressDialog();
                showLoad.postValue(false);
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    //获取详情
    public void getDetail(String no) {
        showLoad.postValue(true);
        APIManager.getInstance().getMachineAPI().getDetailFactory(no).enqueue(new Callback<BaseEntity<AskLeaseFactoryEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<AskLeaseFactoryEntity>> call, Response<BaseEntity<AskLeaseFactoryEntity>> response) {
                showLoad.postValue(false);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    showDetailLiveData.postValue(response.body().getData());
                } else {
                    msgLiveData.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<AskLeaseFactoryEntity>> call, Throwable t) {
                showLoad.postValue(false);
            }
        });
    }

    //  发布求租信息
    public void addOrupdate(AskLeaseFactoryEntity askLeaseFactoryEntity) {
        showLoad.postValue(true);
        APIManager.getInstance().getMachineAPI().addOrUpdateFactory(askLeaseFactoryEntity).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                showLoad.postValue(false);
                if (response.body() == null || response.body().getState() == null){
                    msgLiveData.postValue("发布失败");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    //保存发布成功后返回的id,用于查看详情
                    if (response.body().getData() != null && response.body().getData() instanceof String) {
                        publishLiveData.postValue((String) response.body().getData());
                    }else{
                        msgLiveData.postValue("发布失败");
                    }
                } else {
                    msgLiveData.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                msgLiveData.postValue("发布失败");
            }
        });
    }

    //获取类型分类数据
    public void getTypeModel() {
        APIManager.getInstance().getLeaseAPI().getAllTypeFactory().enqueue(new Callback<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>> call, Response<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData() && null != response.body().getData()) {
                        showTypeViewLiveData.postValue(response.body().getData());
                    } else {
                        msgLiveData.postValue("暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>> call, Throwable t) {
                Log.v("dd", "sdsdd");
            }
        });
    }

    // 获取付款方式
    public void getAllPayWay() {
        APIManager.getInstance().getMachineAPI().getAllPayWay().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        showAllPayWayLiveData.postValue(response.body().getData());
                    } else {
                        msgLiveData.postValue("暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    // 获取供电方式
    public void getPowerWay() {
        APIManager.getInstance().getMachineAPI().getSupplyConfig().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        showPowerWayLiveData.postValue(response.body().getData());
                    } else {
                        msgLiveData.postValue("暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    // 获取办公区域、停车区域、住宿区域、卸货平台配置信息
    public void getHasNotConfig() {
        APIManager.getInstance().getMachineAPI().getHasNotConfig().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        showPurposeListLiveData.postValue(response.body().getData());
                    } else {
                        msgLiveData.postValue("暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }




}
